import iview from 'view-design';
import initToken from "@/libs/util.token.js";
const custInfo = initToken.initToken()

export const modal = (item,url,visible) => {
  //  return {
  //    template: `
  //    <Modal :title="item.attachmentName" v-model="visible" style="z-index:10000">
  //       <img :src="url" v-if="visible" style="width: 100%" />
  //     </Modal>
  //    `,
  //    name: "imgModal",
  //    props: {
  //     visible: {
  //       type: Boolean,
  //       default: falsefattachmentView
  //     }
  //    },
  //    data() {
  //      return {
  //       url,
  //       item,
  //       visible
  //      }
  //    }
  //  }
  iview.Modal.width = "800px"
  iview.Modal.confirm({
    render: (h) => {
        return h('img', {
            attrs: {
                src: url
            },
            style: {
                width: "100%",
                display: "inline-block"
            }        
        })
    }
},)
}


export const formatTime = (key,h,params) => {
    if(params.row[key]) {
    let str = params.row[key].split(" ") 
    return (
      <div>
        <span>
        {
          str[0] 
        }
        </span>
        <br />
        <span>
           {
          str[1] 
        }
        </span>
      </div>
    )
    }
  }

export const attachmentView =  (_this,tableName=null, code="bAttachmentInfoList",h, params) => {

 
  if (
    params.row[code] &&
    params.row[code].length > 0
  ) {
    
    return (
      <div>
        {params.row[code].map(item => {
          if(item.downloading) {
            return (

            <div
                style={{
                  cursor: "not-allowed",                       
                  display: "inline-block",
                  position: "relative"
                }}
              >
                {/* <Spin> */}
                  <Icon
                    type="ios-loading"
                    size="20"
                    class="demo-spin-icon-load"
                  ></Icon>
                {/* </Spin> */}
              </div>
            )
          }else{
            
            return (
            <div
              onclick={_this.filePreview.bind(_this, item, params,tableName)}
              style={{ cursor: "pointer",display: "inline-block" }}
            >
              {
                sliceString(item.attachmentName.split(".")[1])
                ?<Icon type="ios-image-outline" color="rgb(64,198,255)" size="25" />
                :<Icon custom="iconfont zhny-icon-fujian1" color="rgb(64,198,255)" size="25" />
                
              }
              
            </div>
          );
          }
          
        })}
      </div>
    );
  }
}

const sliceString = (extension) => {
  return (
    extension === "jpg" ||
    extension === "jpeg" ||
    extension === "gif" ||
    extension === "png"
  );
}

export const numRender = (code, h, params) => {
  return (
    <span>{params.row[code] !== null?params.row[code]:"-"}</span>
  )
}

export const evaluate = (_this, h, params) => {
  params.row.degreeSatisfaction = params.row.degreeSatisfaction?Number(params.row.degreeSatisfaction):0
  let disabled = true
  if(custInfo.groupCode &&
  (custInfo.groupCode === "9999" ||
    custInfo.groupCode === "0200")) {
      disabled = true
  }else{
    if(params.row.isEvaluated==="0" && (params.row.workStatus==="04" || params.row.workStatus==="06")) {
      disabled = false
    }
  }
  
  return (
    <div>
      <Rate
        // class="rate-size-resize"
        value={params.row.degreeSatisfaction ? params.row.degreeSatisfaction : 0}
        show-text
        allow-half
        disabled={disabled}
        onOn-change={_this.evaluateChange.bind(_this, params)}
      >
        <span style="color: #f5a623">{params.row.degreeSatisfaction}分</span>
      </Rate>
    </div>
  );
}

export const dateOption = (type) => {
    switch (type) {
      case 'all':
        return {
          shortcuts: [{
                text: '最近一周',
                value() {
                  const end = new Date();
                  const start = new Date();
                  start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
                  return [start, end];
                }
              },
              {
                text: '最近一月',
                value() {
                  const end = new Date();
                  const start = new Date();
                  start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
                  return [start, end];
                }
              },
              {
                text: '最近三月',
                value() {
                  const end = new Date();
                  const start = new Date();
                  start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
                  return [start, end];
                }
              }
            ],
            disabledDate(date) {
              return date && date.valueOf() < Date.now() - 86400000 + 3600 * 1000 * 24;
            }
        }
        break;
      case 'shortcuts':
        return {
          shortcuts: [{
              text: '最近一周',
              value() {
                const end = new Date();
                const start = new Date();
                start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
                return [start, end];
              }
            },
            {
              text: '最近一月',
              value() {
                const end = new Date();
                const start = new Date();
                start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
                return [start, end];
              }
            },
            {
              text: '最近三月',
              value() {
                const end = new Date();
                const start = new Date();
                start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
                return [start, end];
              }
            }
          ],
        }
        break;
      case 'disabledDate':
        return {

          disabledDate(date) {
            //当天允许选择，modify sunwei 2020.05.06
            // return date && date.valueOf() < Date.now() - 86400000 + 3600 * 1000 * 24;
            return date && date.valueOf() < Date.now() - 86400000;
          }
        }
        break;
      case 'disabledNowBefore':
        return {

          disabledDate(date) {
            return date && date.valueOf() > Date.now() - 86400000 + 3600 * 1000 * 24;
          }
        }
        break;
      default:
        break;
    }

 
}

